package org.lds.ers.server;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;

import org.lds.ers.client.StakeService;
import org.lds.ers.client.model.StakeERSCInfo;
import org.lds.ers.client.model.StakeInfo;
import org.lds.ers.server.model.PMF;
import org.lds.ers.server.model.Stake;
import org.lds.ers.server.model.StakeERS;

import javax.jdo.Query;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class StakeServiceImpl extends RemoteServiceServlet implements
		StakeService {

	public Boolean delete(Long id) {
		Boolean result = false;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try
		{
			Stake s = pm.getObjectById(Stake.class, id);

			pm.deletePersistent(s);
			result = true;
		}
		catch (Exception ex) {
			result = false;
		}
		finally {
			pm.close();
		}
		return result ;
	}

	public StakeInfo insert(StakeInfo stake) {
		
		StakeInfo result = null;
		Stake s = new Stake(stake.getStakeName(), stake.getStakeNo(), stake.getRemark(), new Date(), stake.getByWhom());
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try
		{
			pm.makePersistent(s);
			stake.setID(s.getId());
			result = stake;
		}
		catch (Exception ex) {
			result = null;
		}
		finally {
			pm.close();
		}
		
		return result ;
	}

	public Boolean update(StakeInfo stake) {
		Boolean result = false;
		Stake s = new Stake(stake.getStakeName(), stake.getStakeNo(), stake.getRemark(), new Date(), stake.getByWhom());
		s.setID(stake.getId());
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try
		{
			pm.makePersistent(s);
			result = true;
		}
		catch (Exception ex) {
			result = false;
		}
		finally {
			pm.close();
		}
		return result ;
	}
	
	public List<StakeInfo> getAll()
	{
		List<StakeInfo> stakes = new ArrayList<StakeInfo>();
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
	    String query = "select from " + Stake.class.getName() + " order by stakeName asc";
	    List<Stake> ss = (List<Stake>) pm.newQuery(query).execute();
	    for (Stake s : ss) {
	    	StakeInfo si = new StakeInfo();
	    	si.setID(s.getId());
	    	si.setStakeName(s.getStakeName());
	    	si.setStakeNo(s.getStakeNo());
	    	si.setRemark(s.getRemark());
	    	si.setLastUpdate(s.getLastUpdate());
	    	si.setByWhom(s.getByWhom());
	    	stakes.add(si);
	    }
	    
		return stakes;
	}
	

	public StakeERSCInfo insertERSC(StakeERSCInfo ersc){

		StakeERSCInfo result = null;
		StakeERS s = new StakeERS(ersc.getStakeID(), ersc.getUserID(), ersc.getRealName());
		
			
		PersistenceManager pm = PMF.get().getPersistenceManager();
		//Stake stake = pm.getObjectById(Stake.class, s.getStakeID());
		//s.setStake(stake);
		
		try
		{
			pm.makePersistent(s);
			ersc.setID(s.getID());
			result = ersc;
		}
		catch (Exception ex) {
			result = null;
		}
		finally {
			pm.close();
		}
		
		return result ;
	}
	public Boolean updateERSC(StakeERSCInfo ersc) {
		Boolean result = false;
		StakeERS s = new StakeERS(ersc.getStakeID(), ersc.getUserID(), ersc.getRealName());
		s.setID(ersc.getID());
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try
		{
			pm.makePersistent(s);
			result = true;
		}
		catch (Exception ex) {
			result = false;
		}
		finally {
			pm.close();
		}
		return result ;
	}
	public Boolean deleteERSC(Long id) {
		Boolean result = false;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try
		{
			StakeERS s = pm.getObjectById(StakeERS.class, id);

			pm.deletePersistent(s);
			result = true;
		}
		catch (Exception ex) {
			result = false;
		}
		finally {
			pm.close();
		}
		return result ;
	
	}
	public List<StakeERSCInfo> getAllERSC(Long stakeID){
		List<StakeERSCInfo> erscs = new ArrayList<StakeERSCInfo>();
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
	    //String query = "select from " + StakeERS.class.getName() + " order by userID asc";
		Query query = pm.newQuery(StakeERS.class, "stakeID == theStakeID ");
		query.declareParameters("Long theStakeID");

	    List<StakeERS> ss = (List<StakeERS>) query.execute(stakeID);
	    for (StakeERS s : ss) {
	    	StakeERSCInfo si = new StakeERSCInfo();
	    	si.setID(s.getID());
	    	si.setStakeID(s.getStakeID());
	    	si.setUserID(s.getUserID());
	    	si.setRealName(s.getRealName());
	    	
	    	erscs.add(si);
	    }
	    
		return erscs;
	}

}
